<script setup lang="ts">
import type { ProductItem } from '@/api/product/types'
import { useRouter } from 'vue-router'
import { ref } from 'vue'
import { Picture } from '@element-plus/icons-vue' // 引入 Element UI 图片图标

interface Props {
  product?: ProductItem
}

withDefaults(defineProps<Props>(), {
  product: () => ({}) as ProductItem
})

const router = useRouter()
const imageError = ref(false) // 添加图片错误状态

// 跳转到商品详情页
const jumpDetailPage = (skuId: number) => {
  router.push({
    name: 'UserProductDetail',
    query: { skuId: skuId }
  })
}

// 格式化属性值显示
const formatAttrs = (attrValueList: { pnValue: string; pvValue: string }[] | undefined) => {
  if (!attrValueList || attrValueList.length === 0) {
    return ''
  }

  return attrValueList.map((attr) => `${attr.pnValue}:${attr.pvValue}`).join(', ')
}

// 图片加载错误处理
const handleImageError = () => {
  imageError.value = true
}
</script>

<template>
  <el-card
    class="product-card"
    shadow="hover"
    v-if="product"
    @click="jumpDetailPage(product.skuId)"
  >
    <!-- 图片区域 -->
    <div class="img-box">
      <img
        v-if="product.mainImg && !imageError"
        :src="product.mainImg"
        :alt="product.title || '商品图'"
        @error="handleImageError"
      />
      <!-- 使用 Element UI 图片图标作为默认占位 -->
      <div v-else class="img-placeholder">
        <el-icon :size="40" color="#C0C4CC">
          <Picture />
        </el-icon>
      </div>
    </div>

    <!-- 标题 -->
    <div class="title">{{ product.title }}</div>

    <!-- 价格区域 -->
    <div class="price-box">
      <span class="origin">￥{{ product.crossedPrice }}</span>
      <span class="current">¥{{ product.price }}</span>
    </div>

    <!-- 属性值 -->
    <div class="attrs">{{ formatAttrs(product.attrValueList) }}</div>
  </el-card>
</template>

<style scoped>
/* 卡片整体 */
.product-card {
  width: 100%;
  height: 100%;
  cursor: pointer;
  transition:
    transform 0.25s,
    box-shadow 0.25s;
  display: flex;
  flex-direction: column;
}
.product-card:hover {
  transform: translateY(-4px); /* 上浮效果 */
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.15) !important;
}

/* 图片容器 */
.img-box {
  width: 100%;
  aspect-ratio: 1/1; /* 1:1 宽高比 */
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f7fa;
  margin-bottom: 12px;
}

.img-box img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.product-card:hover .img-box img {
  transform: scale(1.05);
}

/* 图片占位符 */
.img-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

/* 标题：两行超出省略 */
.title {
  margin-top: 8px;
  font-size: 14px;
  font-weight: 600;
  line-height: 1.4;
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  flex: 1;
}

/* 价格 */
.price-box {
  margin-top: 6px;
  display: flex;
  flex-direction: column;
}
@media (min-width: 768px) {
  .price-box {
    flex-direction: row;
    align-items: baseline;
  }
}
.origin {
  font-size: 12px;
  color: #999;
  text-decoration: line-through;
  margin-right: 6px;
}
.current {
  font-size: 16px;
  color: #f56c6c;
  font-weight: bold;
}

/* 属性：单行超出省略 */
.attrs {
  margin-top: 4px;
  font-size: 12px;
  color: #909399;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>